package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanKickData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanSecondProcessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserZhenHuanKickDaoImpl extends TempDataObjectDao<UserZhenHuanKickData> {

    public static String getUserZhenHuanKickDataSql = "select * from `t_u_zhenhuan_kick` where `activityId`=? and `userId`=?";
    public List<UserZhenHuanKickData> getUserZhenHuanKickData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserZhenHuanKickData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserZhenHuanKickDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserZhenHuanKickData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateUserZhenHuanKickDataSql = "update `t_u_zhenhuan_kick` set partyLevel=?,openUserId=?,openUserName=?,kickUserId=?,confirmTime=?,attackLevel=?,attackPrestige=?,defendLevel=?,defendPrestige=?,pos=? where activityId=? and userId=? and partyId=? and kickTime=?";
    @Override
    protected boolean updateData(UserZhenHuanKickData userZhenHuanKickData) {
        userZhenHuanKickData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getPartyLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getOpenUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userZhenHuanKickData.getOpenUserName()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getKickUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getConfirmTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getAttackLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getAttackPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getDefendLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getDefendPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getPos()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getPartyId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getKickTime()));
        if (getDb().executeNoneQuery(updateUserZhenHuanKickDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserZhenHuanKickDataSql = "insert into `t_u_zhenhuan_kick`(activityId,userId,partyId,partyLevel,openUserId,openUserName,kickTime,kickUserId,confirmTime,attackLevel,attackPrestige,defendLevel,defendPrestige,pos) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserZhenHuanKickData userZhenHuanKickData) {
        userZhenHuanKickData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getPartyId()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getPartyLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getOpenUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userZhenHuanKickData.getOpenUserName()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getKickTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getKickUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getConfirmTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getAttackLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getAttackPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getDefendLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanKickData.getDefendPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanKickData.getPos()));
        if (getDb().executeNoneQuery(insertUserZhenHuanKickDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserZhenHuanKickData parseData(ResultSet rs) throws SQLException {
        UserZhenHuanKickData userZhenHuanKickData = new UserZhenHuanKickData();
        userZhenHuanKickData.setActivityId(rs.getInt("activityId"));
        userZhenHuanKickData.setUserId(rs.getLong("userId"));
        userZhenHuanKickData.setPartyId(rs.getLong("partyId"));
        userZhenHuanKickData.setPartyLevel(rs.getInt("partyLevel"));
        userZhenHuanKickData.setOpenUserId(rs.getLong("openUserId"));
        userZhenHuanKickData.setOpenUserName(rs.getString("openUserName"));
        userZhenHuanKickData.setKickTime(rs.getLong("kickTime"));
        userZhenHuanKickData.setKickUserId(rs.getLong("kickUserId"));
        userZhenHuanKickData.setConfirmTime(rs.getLong("confirmTime"));
        userZhenHuanKickData.setAttackLevel(rs.getInt("attackLevel"));
        userZhenHuanKickData.setAttackPrestige(rs.getLong("attackPrestige"));
        userZhenHuanKickData.setDefendLevel(rs.getInt("defendLevel"));
        userZhenHuanKickData.setDefendPrestige(rs.getLong("defendPrestige"));
        userZhenHuanKickData.setPos(rs.getInt("pos"));

        return userZhenHuanKickData;
    }
}
